package com.banmao.algorithm.No301To400.No404;

public class Solution1 {

    public int sumOfLeftLeaves(TreeNode root) {

        if(root == null) {
            return 0;
        }

        // 根节点不算叶子节点
        if(root.left == null && root.right == null) {
            return 0;
        }

        // 前序遍历
        int sum = recursive(root);

        return sum;

    }

    private int recursive(TreeNode root) {

        if(root == null) {
            // 递归出口
            return 0;
        }

        int sum = 0;
        if(root.left != null && (root.left.left == null && root.left.right == null)) {
            // 该节点的左孩子是叶子节点
            sum = root.left.val;
        }

        sum = recursive(root.left) + sum;
        sum = recursive(root.right) + sum;

        return sum;

    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
